﻿using System;
using System.CodeDom;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace 形状类
{
    internal class Program
    {
        static void Main(string[] args)
        {
            for (; ; )
            {
                Console.WriteLine("请输入一个数，选择你想要计算的图形：\n1——矩形，2——正方形，3——三角形,4——圆形");
                int tag;
                tag = Convert.ToInt32(Console.ReadLine());
                switch (tag)
                {
                    case 1:
                        Shape s1 = new Rectangle();
                        Console.WriteLine("请输入矩形的长和宽（三个数，其中第三个数请输入0,通过换行区分)：");
                        double x1, y1, z1 = 0;
                        x1 = Convert.ToDouble(Console.ReadLine());
                        y1 = Convert.ToDouble(Console.ReadLine());
                        z1 = Convert.ToDouble(Console.ReadLine());
                        if (!s1.LegalOfShape(x1, y1, z1))
                        {
                            Console.WriteLine("请正确输入长和宽！\n");
                        }
                        else
                            Console.WriteLine("矩形的面积为：{0}\n", s1.AreaOfShape(x1, y1, z1));
                        break;


                    case 2:
                        Shape s2 = new Squre();
                        Console.WriteLine("请输入正方形的边长（三个数，其中第二、三个数请输入0,通过换行区分)：");
                        double x2, y2, z2 = 0;
                        x2 = Convert.ToDouble(Console.ReadLine());
                        y2 = Convert.ToDouble(Console.ReadLine());
                        z2 = Convert.ToDouble(Console.ReadLine());
                        if (!s2.LegalOfShape(x2, y2, z2))
                        {
                            Console.WriteLine("请正确输入边长！\n");
                        }
                        else
                            Console.WriteLine("正方形的面积为：{0}\n", s2.AreaOfShape(x2, y2, z2));
                        break;


                    case 3:
                        Shape s3 = new Triangle();
                        Console.WriteLine("请输入三角形的三条边长（三个数，通过换行区分)：");
                        double x3, y3, z3 = 0;
                        x3 = Convert.ToDouble(Console.ReadLine());
                        y3 = Convert.ToDouble(Console.ReadLine());
                        z3 = Convert.ToDouble(Console.ReadLine());
                        if (!s3.LegalOfShape(x3, y3, z3))
                        {
                            Console.WriteLine("请正确输入边长！\n");
                        }
                        else
                            Console.WriteLine("三角形的面积为：{0}\n", s3.AreaOfShape(x3, y3, z3));
                        break;


                    case 4:
                        Shape s4 = new Circle();
                        Console.WriteLine("请输入圆形的半径（三个数，其中第二、三个数请输入0,通过换行区分)：");
                        double x4, y4, z4 = 0;
                        x4 = Convert.ToDouble(Console.ReadLine());
                        y4 = Convert.ToDouble(Console.ReadLine());
                        z4 = Convert.ToDouble(Console.ReadLine());
                        if (!s4.LegalOfShape(x4, y4, z4))
                        {
                            Console.WriteLine("请正确输入半径！\n");
                        }
                        else
                            Console.WriteLine("圆形的面积为：{0}\n", s4.AreaOfShape(x4, y4, z4));
                        break;


                    default:
                        Console.WriteLine("请输入1，2，3，4中的一个数！\n");
                        break;
                }
            }
        }
    }
}
abstract class Shape
{
    public abstract double AreaOfShape(double x, double y, double z);
    public abstract bool LegalOfShape(double x, double y, double z);
}

// 矩形类
class Rectangle : Shape
{
    public override double AreaOfShape(double x, double y, double z)
    {
        return x * y;
    }
    public override bool LegalOfShape(double x, double y, double z)
    {
        if (x > 0 && y > 0 && z == 0)
            return true;
        else
            return false;
    }
}
// 正方形类
class Squre : Shape
{
    public override double AreaOfShape(double x, double y, double z)
    {
        return x * x;
    }
    public override bool LegalOfShape(double x, double y, double z)
    {
        if (y == 0 && x > 0 && z == 0)
            return true;
        else
            return false;
    }
}

// 三角形类
class Triangle : Shape
{
    public override double AreaOfShape(double x, double y, double z)
    {
        return x * y * Math.Sqrt(1 - ((x * x + y * y - z * z) / (2 * x * y)) * ((x * x + y * y - z * z) / (2 * x * y))) / 2;
    }
    public override bool LegalOfShape(double x, double y, double z)
    {
        if (x + y > z && y + z > x && x + z > y && x > 0 && y > 0 && z > 0)
            return true;
        else
            return false;
    }
}

// 圆形类
class Circle : Shape
{
    public override double AreaOfShape(double x, double y, double z)
    {
        return 3.14 * x * x;
    }
    public override bool LegalOfShape(double x, double y, double z)
    {
        if (x > 0 && y == 0 && z == 0)
            return true;
        else
            return false;
    }
}